package com.zy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zy.entity.Drug;
import com.zy.entity.Role;
import com.zy.service.DrugService;
import com.zy.service.RoleService;
import com.zy.vo.DataView;
import com.zy.vo.DrugVo;
import com.zy.vo.RoleVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/drug")
public class DrugController {
    @Autowired
    private DrugService drugService;

    /**
     * 查询所有的药品带有分页，带有查询药品
     */
    @RequestMapping("/loadAllDrug")
    @ResponseBody
    public DataView loadAllDrug(DrugVo drugVo){
        IPage<Role> page = new Page<>(drugVo.getPage(), drugVo.getLimit());
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(drugVo.getYname()), "name", drugVo.getYname());
//        drugService.page(page,queryWrapper);
        DataView dataView = new DataView(page.getTotal(), page.getRecords());
        dataView.setCode(0);
        return dataView;
    }

    /**
     * 添加药品
     */
    @RequestMapping("/addDrug")
    @ResponseBody
    public DataView addDrug(Drug drug){
        drugService.save(drug);
        DataView dataView = new DataView();
        dataView.setMsg("添加药品成功");
        dataView.setCode(200);
        return  dataView;
    }

    /**
     * 删除药品
     */
    @RequestMapping("/deleteDrug")
    @ResponseBody
    public DataView deleteDrug(Drug drug){
        drugService.removeById(drug.getYid());
        DataView dataView = new DataView();
        dataView.setMsg("删除药品成功");
        dataView.setCode(200);
        return  dataView;
    }

    /**
     * 修改药品
     */
    @RequestMapping("/updateDrug")
    @ResponseBody
    public DataView updateDrug(Drug drug){
        drugService.updateById(drug);
        DataView dataView = new DataView();
        dataView.setMsg("修改药品信息成功");
        dataView.setCode(200);
        return  dataView;
    }



}
